%**************************************************************************
%
%           "Endogenous Liquidity and Capital Reallocation"
%      Cui, W., Wright, R., & Zhu, Y. (2024), Journal of Political Economy
%                       Last Modified: Feb 2024.  
%
%**************************************************************************

%%%% This code produces medium-run prediction shown in the main text

clear; clc; close all

%% -------- Preparation
%%%% 5 year window
window = 5; 
path = '../data';
load param_calibrated
par = par_calibrated;

%%%% Inflaton
fred_CPI    = getFredData('CPIAUCSL', '1970-12-31', '2020-12-31','lin','a','eop');
CPI         = fred_CPI.Data(:, 2);
inflation   = log(CPI(2:end)) - log(CPI(1:end-1)); % inflation measure consistent with empirical analysis


%% -------- Model's long run
load('model_long_run_raw.mat')

interp_method = 'spline';

%%%% moving average ({2} in Table_model below indicates the results under calibrated parameters)
iota_grid         = log(1 + inflation) + log(1 / par.BETA);%convert data inflation to norminal interest rates using fisher equiation to get model prediction
iota_grid         = movmean(iota_grid, [0, window - 1]);
iota_grid         = iota_grid(1: end-2); %%%% note the sample stops at 2018, so we did the adjustment "-2" 

model_iota_grid   = Table_model{2}(:,1) ;
I_Y_model_ma      = interp1(model_iota_grid, Table_model{2}(:,2), iota_grid, interp_method, 'extrap'); %%%% check Table_model_order
C_Y_model_ma      = interp1(model_iota_grid, Table_model{2}(:,3), iota_grid, interp_method, 'extrap');
R_share_model_ma  = interp1(model_iota_grid, Table_model{2}(:,5), iota_grid, interp_method, 'extrap');
P_share_model_ma  = interp1(model_iota_grid, Table_model{2}(:,6), iota_grid, interp_method, 'extrap');

%%%% fix-window  ({2} in Table_model below indicates the results under calibrated parameters)
iota_grid         = log(1 + inflation(1:end-2)) + log(1 / par.BETA); %%%% note the sample stops at 2018, so we did the adjustment "-2" 
for i = 1:length(iota_grid)
    first           = floor((i-1)/window) * window + 1;
    last            = min(first + window - 1, length(iota_grid));
    iota_grid1(i)   = mean(iota_grid(first:last));  
end
R_share_model_fix   = interp1(model_iota_grid, Table_model{2}(:,5), iota_grid1, interp_method, 'extrap');
P_share_model_fix   = interp1(model_iota_grid, Table_model{2}(:,6), iota_grid1, interp_method, 'extrap');


%% -------- Loading data counterparts
load([path '/variables.mat'])
R_share         = variables_itself(:,4);
P_share         = variables_itself(:,5);

for i = 1:length(R_share_model_fix)
    first = floor((i-1)/window)*window+1;
    last  = min(first+window-1,length(R_share));     
    R_share_data_fix(i)     = mean(R_share(first:last));
    P_share_data_fix(i)     = mean(P_share(first:last));
    
end

R_share_data_ma   = movmean(R_share, [0,window-1]);
P_share_data_ma   = movmean(P_share, [0,window-1]);


%% ------- Plotting
time = (1971:2018)' ;
data_vs_model = figure;

subplot(2,2,1)
title('R Share: 5-Year Fix Window Average')
hold on
plot(time, R_share_model_fix,'r', 'linewidth',2)
plot(time, R_share_data_fix,'b', 'linewidth',2, 'linestyle','--')

xlim([1971,2018])
ylim([0,.9])
xlabel('Year')

subplot(2,2,2)
title('P Share: 5-Year Fix Window Average')
hold on
plot(time,P_share_model_fix,'r', 'linewidth',2)
plot(time, P_share_data_fix,'b', 'linewidth',2, 'linestyle','--')
xlim([1971,2018])
ylim([0,.9])
xlabel('Year')

subplot(2,2,3)
title('R Share: 5-Year Rolling Average')
hold on
plot(time,R_share_model_ma,'r', 'linewidth',2)
plot(time, R_share_data_ma,'b', 'linewidth',2, 'linestyle','--')

xlim([1971,2018])
xlabel('Year')
ylim([0,.9])


subplot(2,2,4)
title('P Share: 5-Year Rolling Average')
hold on
plot(time,P_share_model_ma,'r', 'linewidth',2)
plot(time, P_share_data_ma,'b', 'linewidth',2, 'linestyle','--')
xlim([1971,2018])
xlabel('Year')
legend('Model','Data')
ylim([0,.9])

saveas(data_vs_model,'figures/data_model_medium_run','epsc')
saveas(data_vs_model,'figures/data_model_medium_run','png')

